package com.fly.caihong.frag;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fly.caihong.R;
import com.fly.caihong.activity.AttentionMoreActivity;
import com.fly.caihong.activity.TopicDetailActivity;
import com.fly.caihong.adapter.HomeAttentionAdapter;
import com.fly.caihong.base.BasePageListFragment;
import com.fly.caihong.model.HomeAttentionItem;
import com.fly.caihong.model.TopicItem;
import com.fly.caihong.model.UserItem;
import com.fly.caihong.utils.ActivityUtils;
import com.fly.caihong.utils.ApUtils;
import com.fly.caihong.utils.DataHandler;
import com.fly.caihong.utils.GlideUtils;
import com.fly.caihong.utils.SizeUtils;
import com.fly.caihong.utils.ToastUtils;
import com.fly.caihong.view.PullRefreshRecyclerView;

public class HomeAttentionFragment extends BasePageListFragment {
    HomeAttentionItem homeAttentionItem;
    int defaultSize, allSize;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        homeAttentionItem = DataHandler.convertJsonData("home/attention", HomeAttentionItem.class);
        defaultSize = SizeUtils.dp2px(50);
        allSize = SizeUtils.dp2px(30);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        if (homeAttentionItem.getAttentionUserList() == null || homeAttentionItem.getAttentionTopicList() == null) {
            ToastUtils.showLong("关注成员数据和关注帖子数据为空");
            return;
        }
        View searchBar = LayoutInflater.from(getActivity()).inflate(R.layout.layout_search_bar, null);
        EditText edtSearch = searchBar.findViewById(R.id.edt_search);
        edtSearch.setHint("刺绣匠人谈传承：让传统技艺融于现代生活");
        mAdapter.addHeaderView(searchBar);
        View attentionBar = LayoutInflater.from(getActivity()).inflate(R.layout.layout_attention_recycler_view, null);
        mAdapter.addHeaderView(attentionBar);
        edtSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startActivity(getActivity(), AttentionMoreActivity.class);
            }
        });
        RecyclerView mRecyclerView = attentionBar.findViewById(R.id.rv_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()
                , LinearLayoutManager.HORIZONTAL, false));

        mRecyclerView.setAdapter(new BaseQuickAdapter<UserItem, BaseViewHolder>(R.layout.layout_attention_item, homeAttentionItem.getAttentionUserList()) {
            @Override
            protected void convert(BaseViewHolder helper, UserItem item) {
                helper.setText(R.id.tv_name, item.getNickName());
                ImageView ivProfile = helper.getView(R.id.iv_profile);
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) ivProfile.getLayoutParams();
                if ("关注更多".equals(item.getNickName())) {
                    lp.width = allSize;
                    lp.height = allSize;
                    ivProfile.setImageResource(R.drawable.ic_attention_more);
                } else {
                    lp.width = defaultSize;
                    lp.height = defaultSize;
                    GlideUtils.loadCircleAvatar(ApUtils.getApp(), ivProfile, item.getPhoto());
                }
                helper.setOnClickListener(R.id.iv_profile, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }

        });
    }

    @Override
    protected Pair<PullRefreshRecyclerView, BaseQuickAdapter> initPullRefreshView(View view) {
        if (homeAttentionItem == null) {
            homeAttentionItem = new HomeAttentionItem();
        }
        return new Pair(view.findViewById(R.id.pull_refresh_layout), new HomeAttentionAdapter(homeAttentionItem.getAttentionTopicList()));
    }

    @Override
    public void initData() {
        super.initData();
        dismissRefreshStatus();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        TopicDetailActivity.launchActivity(getActivity(), (TopicItem) adapter.getItem(position));
    }

    @Override
    protected void loadPageData(int pageNum) {
        mPullRefreshView.loadMoreEnd();
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_pull_refresh_layout;
    }

    @Override
    public void onRefresh() {
        dismissRefreshStatus();
    }

}
